package achao.gallary.com.normalgallary;

import android.graphics.Rect;
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.ImageView;

/**
 * @author LiuChao
 * @describe
 * @date 2017/3/15
 * @contact email:450127106@qq.com
 */

public class ImageRect implements Parcelable {
    // 图片集中所处位置
    private int position;
    // 是否完全可见
    private boolean isTotalVisible;
    // 当前在屏幕中的位置：-1 全部或部分超出顶部 1：全部或部分超出底部 0：顶部和底部之间
    private int positionState;
    private int toEndgeDistance;// 距离边缘的距离
    private int thumbnailWidth;
    private int thumbnailHegiht;
    private Rect thumbRect;
    public Rect imageViewEntireRect;
    public Rect imageViewVisibleRect;
    public Rect scaledBitmapRect;

    public static ImageRect buildImageRect(ImageView imageView, int position) {
        ImageRect imageRect = new ImageRect();
        imageRect.position = position;
        // 直接根据控件，设置高度
        imageRect.thumbnailHegiht = imageView.getMeasuredHeight();
        imageRect.thumbnailWidth = imageView.getMeasuredWidth();

        imageRect.imageViewEntireRect = new Rect();
        int[] location = new int[2];
        // imageView最左边和最上边的位置
        imageView.getLocationOnScreen(location);
        imageRect.imageViewEntireRect.left = location[0];
        imageRect.imageViewEntireRect.top = location[1];
        // imageView最右边和最下边的位置
        imageRect.imageViewEntireRect.right = imageRect.imageViewEntireRect.left + imageView.getWidth();
        imageRect.imageViewEntireRect.bottom = imageRect.imageViewEntireRect.top + imageView.getHeight();

        imageRect.imageViewVisibleRect = new Rect();
        boolean isVisible = imageView.getGlobalVisibleRect(imageRect.imageViewVisibleRect);

        boolean checkWidth = imageRect.imageViewVisibleRect.width() < imageView.getWidth();
        boolean checkHeight = imageRect.imageViewVisibleRect.height() < imageView.getHeight();
        // 是否完全可见
        imageRect.isTotalVisible = isVisible && !checkWidth && !checkHeight;
        // 设置距离
        if (!imageRect.isTotalVisible) {
            if (imageRect.imageViewEntireRect.top < 0) {
                // 在顶部
                imageRect.positionState = -1;
            } else {
                imageRect.positionState = 1;
            }
            imageRect.toEndgeDistance = imageView.getWidth() - imageRect.imageViewVisibleRect.width();
        } else {
            imageRect.positionState = 0;
        }

        return imageRect;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public boolean isTotalVisible() {
        return isTotalVisible;
    }

    public void setTotalVisible(boolean totalVisible) {
        isTotalVisible = totalVisible;
    }

    public int getPositionState() {
        return positionState;
    }

    public void setPositionState(int positionState) {
        this.positionState = positionState;
    }

    public int getToEndgeDistance() {
        return toEndgeDistance;
    }

    public void setToEndgeDistance(int toEndgeDistance) {
        this.toEndgeDistance = toEndgeDistance;
    }

    public int getThumbnailWidth() {
        return thumbnailWidth;
    }

    public void setThumbnailWidth(int thumbnailWidth) {
        this.thumbnailWidth = thumbnailWidth;
    }

    public int getThumbnailHegiht() {
        return thumbnailHegiht;
    }

    public void setThumbnailHegiht(int thumbnailHegiht) {
        this.thumbnailHegiht = thumbnailHegiht;
    }

    public Rect getThumbRect() {
        return thumbRect;
    }

    public void setThumbRect(Rect thumbRect) {
        this.thumbRect = thumbRect;
    }

    public Rect getImageViewEntireRect() {
        return imageViewEntireRect;
    }

    public void setImageViewEntireRect(Rect imageViewEntireRect) {
        this.imageViewEntireRect = imageViewEntireRect;
    }

    public Rect getImageViewVisibleRect() {
        return imageViewVisibleRect;
    }

    public void setImageViewVisibleRect(Rect imageViewVisibleRect) {
        this.imageViewVisibleRect = imageViewVisibleRect;
    }

    public Rect getScaledBitmapRect() {
        return scaledBitmapRect;
    }

    public void setScaledBitmapRect(Rect scaledBitmapRect) {
        this.scaledBitmapRect = scaledBitmapRect;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.position);
        dest.writeByte(this.isTotalVisible ? (byte) 1 : (byte) 0);
        dest.writeInt(this.positionState);
        dest.writeInt(this.toEndgeDistance);
        dest.writeInt(this.thumbnailWidth);
        dest.writeInt(this.thumbnailHegiht);
        dest.writeParcelable(this.thumbRect, flags);
        dest.writeParcelable(this.imageViewEntireRect, flags);
        dest.writeParcelable(this.imageViewVisibleRect, flags);
        dest.writeParcelable(this.scaledBitmapRect, flags);
    }

    public ImageRect() {
    }

    protected ImageRect(Parcel in) {
        this.position = in.readInt();
        this.isTotalVisible = in.readByte() != 0;
        this.positionState = in.readInt();
        this.toEndgeDistance = in.readInt();
        this.thumbnailWidth = in.readInt();
        this.thumbnailHegiht = in.readInt();
        this.thumbRect = in.readParcelable(Rect.class.getClassLoader());
        this.imageViewEntireRect = in.readParcelable(Rect.class.getClassLoader());
        this.imageViewVisibleRect = in.readParcelable(Rect.class.getClassLoader());
        this.scaledBitmapRect = in.readParcelable(Rect.class.getClassLoader());
    }

    public static final Parcelable.Creator<ImageRect> CREATOR = new Parcelable.Creator<ImageRect>() {
        @Override
        public ImageRect createFromParcel(Parcel source) {
            return new ImageRect(source);
        }

        @Override
        public ImageRect[] newArray(int size) {
            return new ImageRect[size];
        }
    };
}
